//
//  BDViewController.m
//  BDDynamicGridViewDemo
//
//  Created by Nor Oh on 6/21/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#define UIAlertViewTag_Unlock 0x1

#import "BDViewController.h"
#import "BDViewController+Private.h"
#import "BDRowInfo.h"
#import "FullScreenImageControllerViewController.h"
//#import "CardViewController.h"
//#import "BlockerViewController.h"
#import "ADManage.h"
#import "GameControllerViewController.h"


@interface BDViewController (){
}

@end

@implementation BDViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationController.navigationBar.barStyle = UIBarStyleBlackOpaque;
    self.delegate = self;
    
    self.onLongPress = ^(UIView* view, NSInteger viewIndex){

//        BlockerViewController* blocker = [[BlockerViewController alloc]initWithNibName:nil bundle:nil];
//        [self.navigationController pushViewController:blocker animated:YES];

    };

    self.onSingleTap = ^(UIView* view, NSInteger viewIndex){
        
        if ([[GameControllerViewController sharedGameControllerInstance] IsPassedLevel:viewIndex])
        {
            FullScreenImageControllerViewController* ful = [[FullScreenImageControllerViewController alloc]initWithNibName:nil bundle:nil];
            ful.showImage = (UIImageView*)view;
            [self.navigationController pushViewController:ful animated:YES];
        }
        else
        {
            int usergold = [[GameControllerViewController sharedGameControllerInstance] GetUserGold];
            if(usergold >= 5)
            {
                UIAlertView * al = [[UIAlertView alloc] initWithTitle:nil message:@"尚未解锁，花费5点墨水才能继续！" delegate:self cancelButtonTitle:@"NO" otherButtonTitles:@"YES", nil];
                al.tag = (viewIndex << 4)|UIAlertViewTag_Unlock;
                [al show];
            }
            else
            {
                [[[UIAlertView alloc] initWithTitle:nil message:@"您的墨水不够了，请继续玩游戏关卡或者支持我们的广告。" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil] show];
            }
        }
    };
    
    self.onDoubleTap = ^(UIView*view,NSInteger viewIndex){
    
//    CardViewController* card = [[CardViewController alloc] initWithNibName:nil bundle:nil];
//    [self.navigationController pushViewController:card animated:YES];

        
    };
    
    adButton = [[UIButton alloc] init];
    [adButton setTitle:@"免费墨水" forState:UIControlStateNormal];
    [adButton addTarget:self action:@selector(adButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    [adButton setBackgroundColor:[UIColor blueColor]];
    [adButton setFrame:CGRectMake(0, 0, 100, 40)];
    
    [self.view addSubview:adButton];
    
    [[ADManage shareManage] showAdWith:self.view];
    [self _demoAsyncDataLoading];
    [self buildBarButtons];
    
}

- (void)adButtonPressed
{
    [[ADManage shareManage] pushTheAdWallWith:self.navigationController];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    //[self reloadData];
    [self refresh];

}

- (void)animateReload
{
    _items = [NSArray new];
    [self _demoAsyncDataLoading];
}

- (NSUInteger)numberOfViews
{
    return _items.count;
}

-(NSUInteger)maximumViewsPerCell
{
    return MAXIMumViewsPerCell;
}

- (UIView *)viewAtIndex:(NSUInteger)index rowInfo:(BDRowInfo *)rowInfo
{
    UIImageView * imageView = [_items objectAtIndex:index];
    return imageView;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    //Call super when overriding this method, in order to benefit from auto layout.
    [super shouldAutorotateToInterfaceOrientation:toInterfaceOrientation];
    return YES;
}

- (CGFloat)rowHeightForRowInfo:(BDRowInfo *)rowInfo
{
//    if (rowInfo.viewsPerCell == 1) {
//        return 125  + (arc4random() % 55);
//    }else {
//        return 100;
//    }
    return 55 + (arc4random() % 125);
}

// Called when a button is clicked. The view will be automatically dismissed after this call returns
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    int viewtag = alertView.tag & 0x0f;
    if (viewtag == UIAlertViewTag_Unlock) 
    {
        if (buttonIndex == 1)
        {
            int usergold = [[GameControllerViewController sharedGameControllerInstance] GetUserGold];
            if (usergold >= 5)
            {
                usergold -= 5;
                int levelindex = ((alertView.tag & 0xffffffff0)>>4);
                
                [[GameControllerViewController sharedGameControllerInstance] SetUserGold:usergold];
                //[[GameControllerViewController sharedGameControllerInstance] SetUserPassedLevel:levelindex];
                
                UIImageView * selectView = [_items objectAtIndex:levelindex];
                FullScreenImageControllerViewController* ful = [[FullScreenImageControllerViewController alloc]initWithNibName:nil bundle:nil];
                ful.showImage = selectView;
                ful.showImage.tag = levelindex;
                [self.navigationController pushViewController:ful animated:YES];
            }
        }
    }
}

// Called when we cancel a view (eg. the user clicks the Home button). This is not called when the user clicks the cancel button.
// If not defined in the delegate, we simulate a click in the cancel button
- (void)alertViewCancel:(UIAlertView *)alertView
{
    
}


@end
